//
//  ViewController.m
//  ColumnWidget
//
//  Created by -_Asuna on 16/8/7.
//  Copyright © 2016年 David. All rights reserved.
//

#import "ColumnWidget.h"

#define COLUMN_HEIGHT  40
#define BUTTON_COUNT_IN_ONE_SCREEN   5
#define SCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
#define BUTTON_WIDTH  SCREEN_WIDTH/BUTTON_COUNT_IN_ONE_SCREEN



@interface ColumnWidget ()

@end

@implementation ColumnWidget{
    CAAnimationGroup *_oldAnimation;
    CAAnimationGroup *_newAnimation;

}

#pragma mark- init sets

//初始化titleArray
-(void)setTitleArray:(NSArray *)array{
    _titleArray=array;
}
//根据titleArray设置_items，同时为_scorllView添加subView
-(void)initItem{
    for(NSString *title in _titleArray){
        NSUInteger index=[_titleArray indexOfObject:title];
        UIButton *button=[self setItem:title withTag:index];
        [_scrollView addSubview:button];
        [_items addObject:button];
    }
  
}
//初始化设置
//初始化_scrollView 设置_scroView的frame，并关闭水平滚动条
//初始化_items
//设置默认背景颜色，字体颜色，被选中时字体颜色
-(void) initSets{
    CGFloat width=[UIScreen mainScreen].bounds.size.width;
    _scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, COLUMN_HEIGHT)];
    [_scrollView setShowsHorizontalScrollIndicator:NO];
    [self setContent];
    _items=[[NSMutableArray alloc] init];
    _lastButton=nil;
    _normalFont=[UIFont systemFontOfSize:17.0];
    _selectedFont=[UIFont systemFontOfSize:20.0];
    _oldAnimation=[CAAnimationGroup animation];
    _newAnimation=[CAAnimationGroup animation];
    self.backgroundColor=[UIColor whiteColor];
    self.tintColor=[UIColor blackColor];
    self.selectedColor=[UIColor redColor];
    self.view=_scrollView;
}

//设置_scrollView的contentSize，contentInset
-(void)setContent{
    CGSize size=CGSizeMake(_titleArray.count*BUTTON_WIDTH, 0);              // set content size
    [_scrollView setContentSize:size];
}
#pragma mark- UIViewController methods
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSets];
    [self initItem];
}


#pragma mark-  button methods
//初始化button，设置frame，title，titleColor，backgroundColor，绑定事件
-(UIButton *)setItem:(NSString *)title withTag:(NSUInteger)tag{
    UIButton *item=[UIButton buttonWithType:UIButtonTypeSystem];
    [item addTarget:self action:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside];
    CGRect frame=CGRectMake(tag*BUTTON_WIDTH, 0, BUTTON_WIDTH, COLUMN_HEIGHT);
    [item setFrame:frame];
    [self setButtonTextLayer:item withTitle:title];
    return item;
}
-(void)setButtonTextLayer:(UIButton *)button withTitle:(NSString *)title{
    CATextLayer *layer=[CATextLayer layer];
    layer.contentsScale=2.0;
    CGRect frame=button.bounds;
    frame.origin.y=10;
    layer.frame=frame;
    layer.foregroundColor=[UIColor blackColor].CGColor;
    layer.backgroundColor=[UIColor whiteColor].CGColor;
    CFStringRef fontName=(__bridge CFStringRef)_normalFont.fontName;
    CGFontRef fontRef=CGFontCreateWithFontName(fontName);
    layer.font=fontRef;
    layer.fontSize=_normalFont.pointSize;
    layer.alignmentMode=kCAAlignmentCenter;
    layer.string=title;
    [button.layer addSublayer:layer];
    [self setAnimationForLayer:layer];
    CGFontRelease(fontRef);
}
-(void)tapButton:(UIButton *)button {
    if(!_lastButton)
        _lastButton=button;
    [self fontAnimateFromIndex:[_items indexOfObject:_lastButton] toIndex:[_items indexOfObject:button] scale:0.5];
    [self setLocationWithIndex:[_items indexOfObject:button]];
    [self.delegate isShowingIndex:[_items indexOfObject:button]];
    _lastButton=button;
}


-(void)fontAnimateFromIndex:(NSInteger)sourceIndex toIndex:(NSInteger)destIndex scale:(CGFloat)scale{
    BOOL quickMode=NO;
    if(scale>1.0) quickMode=YES;
    if(sourceIndex==destIndex)
        return ;
      //NSLog(@"%ld-------->%ld",sourceIndex,destIndex);
    UIButton *button=[_items objectAtIndex:destIndex];
    UIButton *lastButton=[_items objectAtIndex:sourceIndex];
    CATextLayer *lastLayer=(CATextLayer *)lastButton.layer.sublayers[0];
    CATextLayer *layer=(CATextLayer *)button.layer.sublayers[0];
  
    if(quickMode){
        scale*=4;
        scale=(CGFloat)((int)(scale*100)%100)/100+(int)scale%4;
        layer.timeOffset=scale;
        lastLayer.timeOffset=4-scale;
    }else {
    scale*=4;
    scale=(CGFloat)((int)(scale*100)%100)/100+(int)scale%4;
    layer.timeOffset=scale;
    lastLayer.timeOffset=4-scale;
    }
}

-(void)setAnimationForLayer:(CATextLayer *)layer{
    //size animation
    CABasicAnimation *fontSizeAnimation=[CABasicAnimation animationWithKeyPath:@"fontSize"];
    fontSizeAnimation.fillMode=kCAFillModeBackwards;
    fontSizeAnimation.fromValue=@(_normalFont.pointSize);
    fontSizeAnimation.toValue=@(_selectedFont.pointSize);
    fontSizeAnimation.removedOnCompletion=NO;
    //frame animation
    CGPoint position=layer.position;
    CABasicAnimation *frameAnimation=[CABasicAnimation animationWithKeyPath:@"position"];
    frameAnimation.fromValue=[NSValue valueWithCGPoint:position];
    position.y=27;
    frameAnimation.toValue=[NSValue valueWithCGPoint:position];
    frameAnimation.removedOnCompletion=NO;
    //color animation
    CABasicAnimation *colorAnimation=[CABasicAnimation animationWithKeyPath:@"foregroundColor"];
    colorAnimation.fromValue=(id)_tintColor.CGColor;
    colorAnimation.toValue=(id)_selectedColor.CGColor;
    colorAnimation.removedOnCompletion=NO;
    _newAnimation.animations=@[fontSizeAnimation,frameAnimation,colorAnimation];
    _newAnimation.delegate=self;
    _newAnimation.duration=6;
    _newAnimation.removedOnCompletion=YES;
    [layer addAnimation:_newAnimation forKey:@"hello"];
    layer.speed=0.0;
}
-(void)animationDidStart:(CAAnimation *)anim{
    NSLog(@"start");
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    NSLog(@"end");
}

-(void)setLocationWithIndex:(NSUInteger)index{
    CGPoint offset=[_scrollView contentOffset];
    if(index<3){
        offset.x=0;
    }else if(index>[_items count]-4){
        offset.x=([_items count]-5)*BUTTON_WIDTH;
    }else{
        offset.x=index*BUTTON_WIDTH-SCREEN_WIDTH/2+BUTTON_WIDTH/2;
    }
    [_scrollView setContentOffset:offset animated:YES];
}


@end








